import {takeEvery,put} from 'redux-saga/effects';
import {GET_CATEGORY_DATA,GET_ARTICLE_DATA,GET_SHARE_DATA,GET_DATA_BY_CATEGORY,GET_DATA_BY_TIME,GET_DATA_BY_SEARCH,GET_DETAIL_BY_ID} from '../actionTypes'
import {receiveCateGoryData,receiveArticleData,receiveShareData,receiveDataByCategory,receiveDataByTime,receiveDataBySearch,receiveDetailById} from '../actionCreator'
import r from '../../utils/axios'
export const mysaga = function* mysaga(){
    yield takeEvery(GET_CATEGORY_DATA, getListData);
}
function* getListData(){
    let res = yield r.get('/blog/getCate');
    var action = receiveCateGoryData(res.data);
    yield put(action);
}
export const mysaga1 = function* mysaga1(){
    yield takeEvery(GET_ARTICLE_DATA,getArticleData);
}
function* getArticleData(action){
    var res = yield r.post('/blog/selectByType/'+action.data);
    if(res.data.hasOwnProperty('result')){
        var action1 = receiveArticleData([]);
        yield put(action1);
    }else{
        var action2 = receiveArticleData(res.data);
        yield put(action2)
    }
}
export const mysaga2 = function* mysaga2(){
    yield takeEvery(GET_SHARE_DATA,getShareData);
}
function* getShareData(action){
    let data =JSON.stringify({'type':action.data});
    //console.log(data);
    //这边使用json交互，需要设置content-type
    var res = yield r.post('/blog/selectShareData',data,{headers: {'Content-Type': 'application/json;charset=UTF-8'}});
    //console.log(res);
    if(res.data.hasOwnProperty('result')){
        var action1 = receiveShareData([]);
        yield put(action1);
    }else{
        var action2 = receiveShareData(res.data);
        yield put(action2)
    }
}
export const mysaga3 = function* mysaga3(){
    yield takeEvery(GET_DATA_BY_CATEGORY,getDataByCategory);
}
function* getDataByCategory(action){
    var res = yield r.post('/blog/selectByCate/'+action.data);
    if(res.data.hasOwnProperty('result')){
        var action1 = receiveDataByCategory([]);
        yield put(action1);
    }else{
        var action2 = receiveDataByCategory(res.data);
        yield put(action2)
    }
}
export const mysaga4 = function* mysaga4(){
    yield takeEvery(GET_DATA_BY_TIME,getDataByTime);
}
function* getDataByTime(action){
    var res = yield r.post('/blog/selectByTime/'+action.data);
    if(res.data.hasOwnProperty('result')){
        var action1 = receiveDataByTime([]);
        yield put(action1);
    }else{
        var action2 = receiveDataByTime(res.data);
        yield put(action2);
    }
}
export const mysaga5 = function* mysaga5(){
    yield takeEvery(GET_DATA_BY_SEARCH,getDataBySearch);
}
function* getDataBySearch(action){
    var res = yield r.post('/blog/selectBySearch/'+action.data);
    //console.log(res);
    if(res.data.hasOwnProperty('result')){
        var action1 = receiveDataBySearch([]);
        yield put(action1);
    }else{
        var action2 = receiveDataBySearch(res.data);
        yield put(action2);
    }
}
export const mysaga6 = function* mysaga6(){
    yield takeEvery(GET_DETAIL_BY_ID,getDetailById);
}
function* getDetailById(action){
    var res = yield r.post('/blog/selectById/'+action.data);
    //console.log(res)
    var action1 = receiveDetailById(res.data);
    yield put(action1);
}